package com.dcy.znoa.controller.publiz;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.enums.ResultCode;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.company.AddCompanyModel;
import com.dcy.znoa.controller.publiz.model.company.UpdateCompanyModel;
import com.dcy.znoa.controller.publiz.model.companyAddress.AddCompanyAddressModel;
import com.dcy.znoa.controller.publiz.model.companyAddress.SelectCompanyAddressModel;
import com.dcy.znoa.controller.publiz.model.companyAddress.UpdateCompanyAddressModel;
import com.dcy.znoa.domain.Oper;
import com.dcy.znoa.domain.publiz.Company;
import com.dcy.znoa.domain.publiz.CompanyAddress;
import com.dcy.znoa.service.publiz.CompanyAddressService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import static com.dcy.znoa.common.util.pojo.JsonCommon.*;

/**
 * <p>
 * 企业地址表：目前用于与物业交互时使用，只有一个是默认的地址。 前端控制器
 * </p>
 *
 * @author cook
 * @since 2018-05-24
 */
@Controller
@RequestMapping("/companyAddress")
public class CompanyAddressController extends BaseController {

    @Autowired
    CompanyAddressService companyAddressService;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(@Valid SelectCompanyAddressModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
        Oper oper = getCurrentUser();
        //因为分页，所以不得已selectList 方法才返回 JsonForList
        return companyAddressService.selectList(oper, model);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddCompanyAddressModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return new JsonCommon("404", bindingResult.getFieldError().getDefaultMessage());
        }
        CompanyAddress cp = new CompanyAddress();
        if (getCurrentUser() != null && getCurrentUser().getOperId() != null && getCurrentUser().getOperId() > 0) {
            cp.setCreateOperId(getCurrentUser().getOperId());
            cp.setCreateTime(new Date());
            cp.setValidFlag(1);
        }
        BeanUtils.copyProperties(cp, model);
        int result = companyAddressService.insert(cp);
        if (result<0) {
            return new JsonCommon(INSERT_ERROR_CODE);
        }
        return SUCCESS;
    }
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateById")
    public JsonCommon updateById(@Valid UpdateCompanyAddressModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        CompanyAddress cp = new CompanyAddress();
        cp.setUpdateOperId(getCurrentUser().getOperId());
        cp.setUpdateTime(new Date());
        BeanUtils.copyProperties(cp, model);
        int result = companyAddressService.updateById(cp);
        if (result<0) {
            return new JsonCommon(UPDATE_ERROR_CODE);
        }
        return SUCCESS;
    }
    //物理删除
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/delById")
    public JsonCommon delById(Long caId) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (caId==null){
            return new JsonCommon(ResultCode.MISS_PARAM.getCode(), ResultCode.MISS_PARAM.getMessage());
        }
        int result = companyAddressService.delById(caId);
        if (result<0) {
            return new JsonCommon(DELETE_ERROR_CODE);
        }
        return SUCCESS;
    }

}
